<?php
namespace App\Repositories;


use App\Models\Coach;
use App\Models\StudentState;
use App\Models\Reply;
use App\Models\School;
use App\Models\Student;
use App\Models\Subscribe;
use App\Models\TcpStudytime;
use App\Models\User;
use Carbon\Carbon;

class StudentStateRepository extends BaseRepository
{
    public $error = '';
    public function __construct(
        StudentState $comment
    )
    {
        $this->model = $comment;
    }

    /**
     * 资源列表
     * @param  array $param 必须传入与模型查询相关的数据
     * @param  string|array $extra 可选额外传入的参数
     * @param  string $size 分页大小（存在默认值）
     * @return Illuminate\Support\Collection
     */
    public function index($param=[], $extra='', $size='10')
    {
        $student_states = $this->model->where('student_id', $extra->id)->where('item_type',3)->limit($size)->offset(($param['page'] - 1) * $size)->orderBy('id', 'desc')->get();
        $count = $this->model->where('student_id', $extra->id)->count();

        if($student_states){

            foreach ($student_states as $comment) {
                $subscribe_info = Subscribe::leftJoin('yy_put_periods','yy_put_periods.id','=','yy_subscribes.putime_id')
                                        ->leftJoin('coachs','coachs.id','=','yy_subscribes.coach_id')
                                        ->select('yy_put_periods.km','yy_put_periods.startime as starttime','yy_put_periods.endtime','coachs.name as coach','coachs.coachnum','yy_put_periods.sum_period as minute')
                                        ->where('yy_subscribes.id', $comment->cid)
                                        ->first();

                $comment->putime = [];

                if($subscribe_info){
                    $comment->putime = [$subscribe_info];
                }
            }
        }


        return ['list' => $student_states, 'count' => $count];

    }



    /**
     * 评价统计
     * @param $coach_id
     * @return array
     */
    public function statistics($student_id)
    {
        $data = [
            "km2_very_good" => $this->model->leftJoin('tcp_studytimes as ts', 'ts.id', '=', 'comments.cid')
                ->where('ts.km', 2)
                ->where('comment',2)
                ->where('student_id',$student_id)->count(),
            "km3_very_good" => $this->model->leftJoin('tcp_studytimes as ts', 'ts.id', '=', 'comments.cid')
                ->where('ts.km',3)
                ->where('comment',2)
                ->where('student_id',$student_id)->count(),
            "km2_good" => $this->model->leftJoin('tcp_studytimes as ts', 'ts.id', '=', 'comments.cid')
                ->where('ts.km', 2)
                ->where('comment',1)
                ->where('student_id',$student_id)->count(),
            "km3_good" => $this->model->leftJoin('tcp_studytimes as ts', 'ts.id', '=', 'comments.cid')
                ->where('ts.km',3)
                ->where('comment',1)
                ->where('student_id',$student_id)->count(),
            "km2_no_good" =>  $this->model->leftJoin('tcp_studytimes as ts', 'ts.id', '=', 'comments.cid')
                ->where('ts.km', 2)
                ->where('comment',0)
                ->where('student_id',$student_id)->count(),
            "km3_no_good" => $this->model->leftJoin('tcp_studytimes as ts', 'ts.id', '=', 'comments.cid')
                ->where('ts.km',3)
                ->where('comment',0)
                ->where('student_id',$student_id)->count(),
            "sum" => $this->model->where('student_id',$student_id)->count(),
        ];

        $data["satisfaction"] = $data['sum'] ? 100-round(((int)$data['km2_no_good'] + $data['km3_no_good'])*100.0/(int)$data['sum']) : 0;
        return $data;
    }



    /**
     * 存储odb
     * @param $inputs
     */
    public function store($inputs, $extra = '')
    {

    }

    /**
     * 更新
     * @param $id
     * @param $input
     * @return mixed
     */
    public function update($id, $input,$extra = '')
    {
        $model = $this->getById($id);
        return $this->save($model,$input);
    }


    /**
     * 编辑特定id资源
     * @param  int $id 资源id
     * @param  string|array $extra 可选额外传入的参数
     * @return Illuminate\Support\Collection
     */
    public function edit($id, $extra)
    {

    }

    /**
     * @param $model
     * @param $inputs
     * @param bool $falg
     * @return mixed
     */
    public function save($model , $inputs , $falg = false)
    {
        return $model;
    }
    public function getByCoachIdList($coach_id)
    {
        return $this->model->leftJoin('tcp_studytimes as ts','ts.id','=','student_state.cid')->leftJoin('students as stu','stu.id','=','student_state.student_id')->where('student_state.coach_id',$coach_id)
            ->select('ts.km','ts.starttime','ts.endtime','ts.duration','stu.name','stu.phone','student_state.*')->orderBy('student_state.id','desc')->get()->toArray();
    }


    /**
     * 驾校投诉列表
     */
    public function getSchoolComplain($school, $param, $page, $size)
    {
        $list_sql = $this->model->leftJoin('students', 'students.id', '=', 'student_state.student_id')
                    ->leftJoin('coachs', 'coachs.id', '=', 'student_state.coach_id')
                    ->select('student_state.*','students.name as stu_name','students.photo','coachs.name as coach_name')
                    ->where('coachs.school_id', $school->id)
                    ->where('student_state.item_type', 3);

        if ($param['name']){
            $list_sql->where(function($sql) use ($param){
                $sql->where('students.name', 'like', '%'.$param['name'].'%');
                $sql->orWhere('coachs.name', 'like', '%'.$param['name'].'%');
            });
        } elseif ($param['start_time'] && $param['end_time']) {
            $list_sql->where('student_state.createtime', '>=', strtotime($param['start_time']));
            $list_sql->where('student_state.createtime', '<=', strtotime($param['end_time']));
        } elseif ($param['start_time']) {
            $list_sql->where('student_state.createtime', '>=', strtotime($param['start_time']));
        } elseif ($param['end_time']) {
            $list_sql->where('student_state.createtime', '<=', strtotime($param['end_time']));
        }

        if($param['type'] == 2){
            $list_sql->whereNotNull('student_state.reply');
        }else{
            $list_sql->whereNull('student_state.reply');
        }

        $complain_list = $list_sql->limit($size)->offset(($page - 1) * $size)->orderBy('student_state.id','desc')->get();

        if($complain_list){

            foreach ($complain_list as $complain){

                $complain->startime = 0;
                $complain->endtime = 0;
                $complain->km = '';

                if($complain->item_type == 1){
                    $km_info = TcpStudytime::select('km','starttime as startime','endtime')->find($complain->cid);
                    if($km_info){
                        $complain->km = $km_info->km;
                        $complain->startime = strtotime($km_info->startime);
                        $complain->endtime = strtotime($km_info->endtime);
                    }
                } elseif($complain->item_type == 3) {
                    $km_info = Subscribe::leftJoin('yy_put_periods', 'yy_put_periods.id', '=', 'yy_subscribes.putime_id')
                        ->select('yy_put_periods.km','yy_put_periods.startime','yy_put_periods.endtime')
                        ->find($complain->cid);
                    if($km_info){
                        $complain->km = $km_info->km;
                        $complain->startime = $km_info->startime;
                        $complain->endtime = $km_info->endtime;
                    }
                }

            }
        }

        return $complain_list;
    }


}